﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Configuration;
using blackhouse.AsyncMail.Business;
using blackhouse.AsyncMail.Business.Data;

namespace blackhouse.AsyncMail.Web {
    public partial class ASyncHtmlMailGenerator : System.Web.UI.Page {

        #region Fields

        private string skin = "ASyncHtmlMail";
        private int requestId = 0;
        private ASyncMail mail;

        #endregion

        #region Event Handlers

        protected override void OnPreInit(EventArgs e) {
            this.ReadParameters();
            this.LoadMasterPage();

            base.OnPreInit(e);
        }

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);

            if (!this.SetInstance())
                Response.End();
            this.MakeLocalTemplate();
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            this.PopulateTemplate();
        }

        #endregion

        #region Methods

        private void PopulateTemplate() {
            string file = this.GetControlSource(this.mail.TemplateId);
            if (!String.IsNullOrEmpty(file)) {
                ASyncHtmlMailTemplate templateControl = this.LoadControl(file) as ASyncHtmlMailTemplate;
                if (templateControl != null) {
                    templateControl.BindObject = this.mail.BindObject;
                    this.pnlMain.Controls.Add(templateControl);
                }
            }
        }

        private string GetControlSource(int templateId) {
            string file = "~/" + this.GetAppDirectory() + "/" + templateId + ".ascx";
            FileInfo fileInfo = new FileInfo(Server.MapPath(file));
            if (fileInfo.Exists)
                return file;
            file = "~/" + this.GetAppDirectory() + "/" + templateId + ".map";
            fileInfo = new FileInfo(Server.MapPath(file));
            if (fileInfo.Exists) {
                using (FileStream fs = fileInfo.OpenRead()) {
                    using (StreamReader sr = new StreamReader(fs)) {
                        file = sr.ReadLine();
                    }
                }
                fileInfo = new FileInfo(Server.MapPath(file));
                if (fileInfo.Exists)
                    return file;
            }
            return String.Empty;
        }

        private void ReadParameters() {
            string ovSkin = ConfigurationManager.AppSettings["ASyncMailSkin"];
            if (!string.IsNullOrEmpty(Request["skin"]) && this.CheckSkinName(Request["skin"]))
                ovSkin = Request["skin"];
            if (!string.IsNullOrEmpty(ovSkin) && this.CheckSkinName(ovSkin))
                this.skin = ovSkin;

            Int32.TryParse(Request["requestId"], out this.requestId);
        }

        private bool CheckSkinName(string skin) {
            return new FileInfo(Server.MapPath("~/" + this.GetAppDirectory() + "/" + skin + ".Master")).Exists;
        }

        private string GetAppDirectory() {
            return GetMailDirectory();
        }

        internal static string GetMailDirectory() {
            string dir = ConfigurationManager.AppSettings["ASyncMailDirectory"];
            if (string.IsNullOrEmpty(dir))
                dir = "ASyncMails";
            return dir;
        }

        private bool SetInstance() {
            this.mail = new ASyncMail(new SqlASyncMailDataAccess(CacheTaskRunner.GetConnectionString()));
            if (!this.mail.FillByRequestId(this.requestId))
                return false;
            return true;
        }

        private void MakeLocalTemplate() {

        }

        private void LoadMasterPage() {
            this.MasterPageFile = "~/" + this.GetAppDirectory() + "/" + this.skin + ".Master";
        }

        #endregion

    }
}